import { Component, ChangeDetectorRef, ChangeDetectionStrategy } from "@angular/core";
import { Subscription } from "rxjs";
import { CompanyService } from "../../company.service";
import { CompanyGlobalCompactService, GCAssesment } from "../companyGlobalCompact.service";
import { CompanyBaseWidget } from "../../shared/companyBaseWidget/companyBase.widget";
import { ActivatedRoute, Router } from "@angular/router";

/**
 * GC widget component
 * 
 * Author: Sorin Spiru
 * Date: 04.03.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: './widget.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class WidgetComponent extends CompanyBaseWidget {
    private companySub: Subscription;
    private gcSub: Subscription;
    public gCAssesment: typeof GCAssesment = GCAssesment;
    public assesment: number;

    constructor(
        private cdRef: ChangeDetectorRef,
        private route: ActivatedRoute,
        private router: Router,
        private companyService: CompanyService,
        private companyGCService: CompanyGlobalCompactService) {
        super(route, router);
    }

    ngOnInit() {
        this.companySub = this.companyService.company.subscribe(company => {
            this.gcSub = this.companyGCService.getByCompany(company.id).subscribe(summary => {
                this.assesment = summary.overallCompanyAssessment;
                this.cdRef.markForCheck();
            });
        });
    }

    ngOnDestroy() {
        this.companySub.unsubscribe();
        this.gcSub.unsubscribe();
    }
}